package com.vulab.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.vulab.domain.Student;
import com.vulab.util.SimpleDBUtil;
import com.xerox.amazonws.sdb.Domain;
import com.xerox.amazonws.sdb.Item;
import com.xerox.amazonws.sdb.ItemAttribute;
import com.xerox.amazonws.sdb.QueryResult;
import com.xerox.amazonws.sdb.SimpleDB;

/* **************************************************************************
 * Author: Srinivas Nelakuditi 
 * Company: VU Lab Inc.
 * URL: http://vulab.com
 * Core Competency: IT Training, Management Training & Career Mentoring
 * Blog URL: http://vulab.com/blog
 * forum URL:http://vulab.com/community
 * Date: March 5th 2009
 * 
 * 
 * Please modify the code as you wish and use it in any
 * commercial or free software and distribute it.
 * please keep the above author information intact.
 * 
 *****************************************************************************/
public class StudentDAOImpl implements StudentDAO {

	public static final String domain = "student";

	public Student createStudent(Student student) throws Exception {
		SimpleDB studentDB = SimpleDBUtil.getDatabase();
		Item studentItem;
		List<ItemAttribute> attributes = new ArrayList<ItemAttribute>();
		Domain domain = studentDB.getDomain(this.domain);
		Random randomGenerator = new Random();
		int id = randomGenerator.nextInt(100000);
		studentItem = domain.getItem(String.valueOf(id));
		student.setId(String.valueOf(id));
		// TODO: use reflection API to get rid of the attributes code here and
		// make it a utility method
		attributes.add(new ItemAttribute("id", student.getId(), false));
		attributes.add(new ItemAttribute("firstName", student.getFirstName(), false));
		attributes.add(new ItemAttribute("lastName", student.getLastName(), false));
		attributes.add(new ItemAttribute("addressLineOne", student.getAddressLineOne(), false));
		attributes.add(new ItemAttribute("addressLineTwo", student.getAddressLineTwo(), false));
		attributes.add(new ItemAttribute("city", student.getCity(), false));
		attributes.add(new ItemAttribute("state", student.getState(), false));
		attributes.add(new ItemAttribute("zip", student.getZip(), false));
		attributes.add(new ItemAttribute("country", student.getCountry(), false));
		attributes.add(new ItemAttribute("workPhone", student.getWorkPhone(), false));
		attributes.add(new ItemAttribute("workPhoneExtension", student.getWorkPhoneExtension(), false));
		attributes.add(new ItemAttribute("fax", student.getFax(), false));
		attributes.add(new ItemAttribute("homePhone", student.getHomePhone(), false));
		attributes.add(new ItemAttribute("cellPhone", student.getCellPhone(), false));
		attributes.add(new ItemAttribute("email", student.getEmail(), false));
		attributes.add(new ItemAttribute("userId", student.getUserId(), false));
		attributes.add(new ItemAttribute("password", student.getPassword(), false));
		attributes.add(new ItemAttribute("dateOfBirth", student.getDateOfBirth(), false));
		attributes.add(new ItemAttribute("salutation", student.getSalutation(), false));
		attributes.add(new ItemAttribute("jobTitle", student.getJobTitle(), false));
		attributes.add(new ItemAttribute("department", student.getDepartment(), false));
		attributes.add(new ItemAttribute("company", student.getCompany(), false));

		studentItem.putAttributes(attributes);

		return student;
	}

	public void updateStudent(Student student) throws Exception {

		SimpleDB studentDB = SimpleDBUtil.getDatabase();
		Domain domain = studentDB.getDomain(this.domain);
		Item studentItem = domain.getItem(String.valueOf(student.getId()));
		if(studentItem==null)
			throw new Exception("Cannot update db as item does not exist in domain with id "+student.getId());
		List<String> attributesToGet = new ArrayList<String>();
		List<ItemAttribute> attrs = studentItem.getAttributes(attributesToGet);
		
		List<ItemAttribute> attributes = new ArrayList<ItemAttribute>();

		attributes.add(new ItemAttribute("id", student.getId(), true));

		attributes.add(new ItemAttribute("salutation", student.getSalutation(), true));

		attributes.add(new ItemAttribute("jobTitle", student.getJobTitle(), true));

		attributes.add(new ItemAttribute("department", student.getDepartment(), true));

		attributes.add(new ItemAttribute("company", student.getCompany(), true));

		attributes.add(new ItemAttribute("firstName", student.getFirstName(), true));

		attributes.add(new ItemAttribute("lastName", student.getLastName(), true));

		attributes.add(new ItemAttribute("addressLineOne", student.getAddressLineOne(), true));

		attributes.add(new ItemAttribute("addressLineTwo", student.getAddressLineTwo(), true));

		attributes.add(new ItemAttribute("city", student.getCity(), true));

		attributes.add(new ItemAttribute("state", student.getState(), true));

		attributes.add(new ItemAttribute("zip", student.getZip(), true));

		attributes.add(new ItemAttribute("country", student.getCountry(), true));

		attributes.add(new ItemAttribute("workPhone", student.getWorkPhone(), true));

		attributes.add(new ItemAttribute("workPhoneExtension", student.getWorkPhoneExtension(), true));

		attributes.add(new ItemAttribute("fax", student.getFax(), true));

		attributes.add(new ItemAttribute("homePhone", student.getHomePhone(), true));

		attributes.add(new ItemAttribute("firstName", student.getFirstName(), true));

		attributes.add(new ItemAttribute("lastName", student.getLastName(), true));

		attributes.add(new ItemAttribute("cellPhone", student.getCellPhone(), true));

		attributes.add(new ItemAttribute("email", student.getEmail(), true));

		attributes.add(new ItemAttribute("userId", student.getUserId(), true));

		attributes.add(new ItemAttribute("password", student.getPassword(), true));

		attributes.add(new ItemAttribute("dateOfBirth", student.getDateOfBirth(), true));

		studentItem.putAttributes(attributes);

	}

	public Student getStudentById(int studentId) throws Exception {
		SimpleDB studentDB = SimpleDBUtil.getDatabase();
		Domain domain = studentDB.getDomain(this.domain);
		Item studentItem = domain.getItem(String.valueOf(studentId));
		Student student = new Student();
		List<String> attributesToGet = new ArrayList<String>();
		List<ItemAttribute> attrs = studentItem.getAttributes(attributesToGet);
		for (ItemAttribute currentAttribute : attrs) {
			if (currentAttribute.getName().equals("id")) {
				student.setId(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("salutation")) {
				student.setSalutation(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("jobTitle")) {
				student.setJobTitle(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("department")) {
				student.setDepartment(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("company")) {
				student.setCompany(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("firstName")) {
				student.setFirstName(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("lastName")) {
				student.setLastName(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("addressLineOne")) {
				student.setAddressLineOne(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("addressLineTwo")) {
				student.setAddressLineTwo(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("city")) {
				student.setCity(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("state")) {
				student.setState(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("zip")) {
				student.setZip(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("country")) {
				student.setCountry(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("workPhone")) {
				student.setWorkPhone(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("workPhoneExtension")) {
				student.setWorkPhoneExtension(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("fax")) {
				student.setFax(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("homePhone")) {
				student.setHomePhone(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("firstName")) {
				student.setFirstName(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("lastName")) {
				student.setLastName(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("cellPhone")) {
				student.setCellPhone(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("email")) {
				student.setEmail(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("userId")) {
				student.setUserId(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("password")) {
				student.setPassword(currentAttribute.getValue());
			} else if (currentAttribute.getName().equals("dateOfBirth")) {
				student.setDateOfBirth(currentAttribute.getValue());
			}
		}
		return student;
	}

	public void deleteStudentById(int studentId) throws Exception {
		SimpleDB studentDB = SimpleDBUtil.getDatabase();
		Domain domain = studentDB.getDomain(this.domain);
		domain.deleteItem(String.valueOf(studentId));
	}

	public void deleteStudent(Student student) throws Exception {
		SimpleDB studentDB = SimpleDBUtil.getDatabase();
		Domain domain = studentDB.getDomain(this.domain);
		domain.deleteItem(student.getId());
	}

	public List<Student> getAllStudents() throws Exception {
		List<Student> studentList = new ArrayList<Student>();
		SimpleDB studentDB = SimpleDBUtil.getDatabase();
		Domain domain = studentDB.getDomain(this.domain);
		List<Item> items;
		QueryResult result = null;
		result = domain.listItems();
		items = result.getItemList();
		List<String> attributesToGet = new ArrayList<String>();
		for (Item studentItem : items) {
			Student student = new Student();
			List<ItemAttribute> attrs = studentItem.getAttributes(attributesToGet);
			for (ItemAttribute currentAttribute : attrs) {
				if (currentAttribute.getName().equals("id")) {
					student.setId(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("salutation")) {
					student.setSalutation(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("jobTitle")) {
					student.setJobTitle(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("department")) {
					student.setDepartment(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("company")) {
					student.setCompany(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("firstName")) {
					student.setFirstName(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("lastName")) {
					student.setLastName(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("addressLineOne")) {
					student.setAddressLineOne(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("addressLineTwo")) {
					student.setAddressLineTwo(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("city")) {
					student.setCity(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("state")) {
					student.setState(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("zip")) {
					student.setZip(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("country")) {
					student.setCountry(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("workPhone")) {
					student.setWorkPhone(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("workPhoneExtension")) {
					student.setWorkPhoneExtension(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("fax")) {
					student.setFax(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("homePhone")) {
					student.setHomePhone(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("firstName")) {
					student.setFirstName(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("lastName")) {
					student.setLastName(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("cellPhone")) {
					student.setCellPhone(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("email")) {
					student.setEmail(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("userId")) {
					student.setUserId(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("password")) {
					student.setPassword(currentAttribute.getValue());
				} else if (currentAttribute.getName().equals("dateOfBirth")) {
					student.setDateOfBirth(currentAttribute.getValue());
				}

			}
			if (student.getId() != null)
				studentList.add(student);
		}

		return studentList;
	}

	public int getTotalCountOfStudents() throws Exception {
		SimpleDB studentDB = SimpleDBUtil.getDatabase();
		Domain domain = studentDB.getDomain(this.domain);
		List<Item> items;
		QueryResult result = null;
		result = domain.listItems();
		items = result.getItemList();
		return items.size();
	}

}